﻿using Apache.NMS;
using Apache.NMS.ActiveMQ;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Send
{
    public partial class Form1 : Form
    {

        private IConnectionFactory factory;
        public Form1()
        {
            InitializeComponent();
            InitProducer();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public void InitProducer()
        {
            try
            {
                //初始化工厂，这里默认的URL是不需要修改的
                factory = new ConnectionFactory("tcp://localhost:61616");
            }
            catch
            {
                lb1.Text = "初始化失败!!";
            }
        }

        private void bt1_Click(object sender, EventArgs e)
        {
            try
            {
                //Create the Connection Factory  
                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");
                using (IConnection connection = factory.CreateConnection())
                {
                    //Create the Session  
                    using (ISession session = connection.CreateSession())
                    {
                        //Create the Producer for the topic/queue  
                        IMessageProducer prod = session.CreateProducer(
                            new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("testing"));
                            //Send Messages  
                            ITextMessage msg = prod.CreateTextMessage();
                            msg.Text = textBox1.ToString();
                            prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
                    }
                }
            }
            catch (System.Exception ex)
            {
            }
        }
    }
}
